package Passageiros;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Vo.StatusAssentoVO;

public class StatusAssentoDAO extends DataSourceDAO
{
	List<StatusAssentoVO> comboStatusAssento = new ArrayList<StatusAssentoVO>();
	public StatusAssentoDAO( ) 
	{
		
	}
	
	//metodo responsavel por retornar todas os Status dos Asentos disponiveis no modelo fisico de dados
	public List<StatusAssentoVO> listarSatatusAssento() throws DAOException
	{
		StatusAssentoVO statusAssento = null;
        String select = "SELECT * FROM STATUSASSENTOS";
        PreparedStatement pstmt	= null;
        ResultSet rs = null;
        
		try
		{
			connect( ) ;
            pstmt = connection.prepareStatement( select );
            //pstmt.setInt( 1, codigo ) ; // Narcisio, verificar esta linha
            rs = pstmt.executeQuery( ) ;
            
            rs.beforeFirst();
                
            while (rs.last() != false)
            {
            	String descricaoStatusAssento = rs.getString("descricao");
            	statusAssento = new StatusAssentoVO( );
            	statusAssento.setDescricaoStatusAssento(descricaoStatusAssento);
                    
            	statusAssento.setCodigoStatusAssento(rs.getInt("codigo"));
                    
            	comboStatusAssento.add(statusAssento);              
            }
         }
		 catch ( SQLException e ) 
		 {
			e.printStackTrace();
			throw new DAOException("Impossivel listar O STATUS DOS ASSENTOS!", e );
		 } 
		 finally 
		 {
		     try 
			 {
				 if ( pstmt != null ) 
                 {
                    pstmt.close();
                 }
                 if ( rs != null ) 
                 {
                    rs.close();
                 }
                 close( ) ;
			 } 
		     catch ( SQLException e ) 
             {
		    	 e.printStackTrace( ) ;
             }
		 }
		 return comboStatusAssento;         
	}

    public StatusAssentoVO consultarStatusAssento( int codigo ) throws DAOException  
    {
    	StatusAssentoVO statusAssento = null;
        String select = "SELECT * FROM STATUSASSENTOS WHERE CODIGO = ?";
        PreparedStatement pstmt	= null;
        ResultSet rs = null;
        
        try 
        {
        	connect( ) ;
            pstmt = connection.prepareStatement( select );
            pstmt.setInt( 1, codigo ) ;
            rs = pstmt.executeQuery( ) ;
            if ( rs.next() ) 
            {
                String descricao = rs.getString("descricao");
                
                statusAssento = new StatusAssentoVO( );
                statusAssento.setCodigoStatusAssento( codigo );
                statusAssento.setDescricaoStatusAssento(descricao);               
            }
        } 
        catch ( SQLException e ) 
        {
            e.printStackTrace();
            throw new DAOException("Impossivel consultar statusAssento!", e );
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                if ( rs != null ) 
                {
                    rs.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
        return statusAssento ;
    }
    
	public void inserirStatusAssento( StatusAssentoVO statusAssento ) throws DAOException  
    {
        String insert = "INSERT INTO STATUSASSENTOS ( CODIGO, DESCRICAO) VALUES (?,?)";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement(insert);
            pstmt.setInt(1,statusAssento.getCodigoStatusAssento());
            pstmt.setString(2,statusAssento.getDescricaoStatusAssento());
           
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel inserir statusAssento!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( );
            }
        }
    }
    
    public void excluirStatusAssento( StatusAssentoVO statusAssento ) throws DAOException  
    {
        String delete = "DELETE FROM STATUSASSENTOS WHERE CODIGO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement( delete );
            pstmt.setInt( 1,statusAssento.getCodigoStatusAssento( ) ) ;
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel deletar statusAssento!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
    }
    
    public void alterarStatusAssento( StatusAssentoVO statusAssento ) throws DAOException  
    {
        String insert = "UPDATE STATUSASSENTOS SET DESCRICAO = ? WHERE CODIGO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement(insert);
            pstmt.setString(1, statusAssento.getDescricaoStatusAssento());
            pstmt.setInt(2, statusAssento.getCodigoStatusAssento());
            pstmt.executeUpdate ();
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel alterar statusAssento!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace();
            }
        }
    }
}
